/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package WS;
import BD.Modulos.BdUsuarios;
import Interfaces.BdException;
import Objects.Rol;
import Objects.Sesion;
import Objects.Usuario;
import java.sql.SQLException;
import javax.annotation.Resource;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.ws.WebServiceContext;
/**
 *
 * @author armgarces
 */
@WebService(serviceName = "Usuarios")
public class Usuarios {
    public final static int CHECK_USER  = 1;    
    @Resource
    WebServiceContext wsContext; 
    /**
     * 
     * @param name
     * @param pswd?
     * @return 
     * @throws Interfaces.BdException 
     */
    @WebMethod(operationName = "logIn")
    public synchronized Sesion logIn(@WebParam(name = "name")String name, 
                                     @WebParam(name = "pswd")String pswd) throws BdException{        
//        boolean boo = Validar.validarLogIn(userName,userPass);
//        if (boo){
    try{
        return (Sesion) BdUsuarios.getInstance().iniciarSesion(name, pswd);        
                //Siempre y cuando no exceda el 3er intento
//                if (Datos.getCont_login() < 3){
//                    Datos.sumCont_login();
                    //Encrypt
//                    String salt = java.util.ResourceBundle.getBundle("BD/DBcon").getString("dns");
//                    userPass = Protector.encrypt(userPass, salt);                    
                    
                    
//                        Datos.setCont_login(0);
//                        Datos.setSesion(sesion);
//                        Datos.getLocalHost();
                        //Solicito lo datos (siempre a travez de un proceso de LN)
//                        ItemLeftBar[] items =  loadMenu();
//                        Datos.setItemLeftBar(items);
//                        Gui.getLeftBar().loadLeftBar(items);    //Solicitud al proceso de impresion grafica
                        //
//                        TopBar.refreshLogImage();
//                        TopBar.refreshSesion(sesion);                        
//                        Gui.getInstance().closeDialog();     
                       
//                }else{                
//                    throw new Exception("Supero el numero de Intentos!\n Por Favor contacte a Sistemas!");
//                }
//            }catch(NullPointerException e){
//                Gui.getInstance().ventanaError("Error Iniciando Sesion!");
//            }catch(Exception e){
//                Gui.getInstance().ventanaError("Error Iniciando Sesion: \n"+e.getMessage());                 
//            }
        }catch(BdException bde){
            throw bde;
        } catch (SQLException ex) {
        }
        return null;
    }
    /**
     * 
     * @param sesion
     * @return 
     * @throws Interfaces.BdException 
     */
    @WebMethod(operationName = "logOut")
    public synchronized boolean logOut(@WebParam(name = "sesion")Sesion sesion) throws BdException{      
        try{
//            Datos.setCont_login(0);
            return BdUsuarios.getInstance().cerrarSesion(sesion);    
//            Datos.setSesion(null);
//            Gui.getLeftBar().clearLeftBar();
//            TopBar.refreshLogImage();
//            TopBar.refreshSesion(null);
//            Gui.getInstance().closeDialog();
//            Screens.getInstance().startIndex(new Stage());            
//        }catch(Exception e){
//            Gui.getInstance().ventanaError("Error Cerrando Sesion: \n"+e.getMessage()); 
//        }
        }catch(BdException bde){
            throw bde;
        } catch (SQLException ex) {
        }
        return false;
    } 
    /***************************************************************************/
    /********************************* USERS ***********************************/
    /***************************************************************************/
    /**
     * 
     * @return 
     * @throws Interfaces.BdException 
     */
    @WebMethod(operationName = "loadUsers")
    public synchronized Usuario[] loadUsers() throws BdException{        
        try{
            return (Usuario[]) BdUsuarios.getInstance().load(new Usuario());      
        }catch(BdException bde){
            throw bde;
        } catch (SQLException ex) {
        }
        return null;
    }  
    /**
     * 
     * @return 
     * @throws Interfaces.BdException 
     */
    @WebMethod(operationName = "loadRols")
    public synchronized Rol[] loadRols() throws BdException {        
        try{
            return (Rol[]) BdUsuarios.getInstance().load(new Rol());                  
        }catch(BdException bde){
            throw bde;
        } catch (SQLException ex) {
        }
        return null;
    }        
    /**
     * 
     * @param user
     * @param usuario
     * @param operacion
     * @return 
     * @throws Interfaces.BdException 
     */
    @WebMethod(operationName = "saveUsuario")
    public synchronized boolean saveUsuario(@WebParam(name = "user")String user,
                                            @WebParam(name = "usuario")Usuario usuario, 
                                            @WebParam(name = "operacion")int operacion) throws BdException {
        
//        boolean boo = Validar.validarSaveUsuario(usuario,pswd);
//        if (boo){
            try{                
//                //Encrypt
//                if(operacion == 1){
//                    String salt = java.util.ResourceBundle.getBundle("BD/DBcon").getString("dns");
//                    usuario.setPswd(Protector.encrypt(pswd, salt));
//                }
            return BdUsuarios.getInstance().save(user, operacion, usuario);
        }catch(BdException bde){
            throw bde;
        } catch (SQLException ex) {
        }
        return false;
    }
    /**
     * 
     * @param name
     * @return 
     * @throws Interfaces.BdException 
     */
    @WebMethod(operationName = "checkUserName")
    public synchronized boolean checkUserName(@WebParam(name = "name")String name) throws BdException {
        try{
            return BdUsuarios.getInstance().check(CHECK_USER,name);                
        }catch(BdException bde){
            throw bde;
        } catch (SQLException ex) {
        }
        return false;
    }
    /**
     * 
     * @param user
     * @param usuario
     * @return 
     * @throws Interfaces.BdException 
     */
    @WebMethod(operationName = "changeUser")
    public synchronized boolean changeUser(@WebParam(name = "user")String user,
                                           @WebParam(name = "usuario")Usuario usuario) throws BdException {        
        try{
            return BdUsuarios.getInstance().change(user,usuario);                
        }catch(BdException bde){
            throw bde;
        } catch (SQLException ex) {
        }
        return false;
    }    
    /**
     * 
     * @param find 
     * @return  
     * @throws Interfaces.BdException  
     */
    @WebMethod(operationName = "findUser")
    public synchronized Usuario[] findUser(@WebParam(name = "find") String find) throws BdException {
        try{
            return (Usuario[]) BdUsuarios.getInstance().find(new Usuario(),find);                  
        }catch(BdException bde){
            throw bde;
        } catch (SQLException ex) {
        }
        return null;
    }
    /**
     * 
     * @param user
     * @param usuario
     * @return 
     * @throws Interfaces.BdException 
     */
    @WebMethod(operationName = "updatePswd")
    public synchronized boolean updatePswd(@WebParam(name = "user")String user, 
                                           @WebParam(name = "usuario")Usuario usuario) throws BdException {        
//        boolean boo = Validar.validar_UpdatePswd(Datos.getUsuario().getPswd(),old,usuario.getPswd(),pswd);
//        if (boo){
        try{                
//                //Encrypt
//                String salt = java.util.ResourceBundle.getBundle("BD/DBcon").getString("dns");
//                usuario.setPswd(Protector.encrypt(pswd, salt));                
            return BdUsuarios.getInstance().save(user, 3, usuario);                                
        }catch(BdException bde){
            throw bde;
        } catch (SQLException ex) {
        }
        return false;
    }
}
